// PayWorking
var app = getApp()
var config = require('config.js');
var netutil = require('netutil.js');
var util = require('util.js');
var orderno = ""; // 下单订单号
var wxOpenid = "";//wxOpenid
var prepayid = "";//wx支付need
var totalMoney = "";
var paytype = '1';//微信1，货到99
var isfrom = '0';
var requestDic = new Object();

//get pay info
/**
 * payorderno：订单号
 *  money:交易金额
 * transactionType://交易类型（国内机票支付 = 1,国内机票退款 = 2,火车票支付 = 5,火车票退款 = 6,
 * subTransactionType://子交易类型（国内机票新订单支付 = 1,火车票新订单支付 = 11,）
 * fromWhere:0:火车票，1:国内机票
 * resultType:0:下单，1:支付
 */
function getPayInfo(payorderno, money,transactionType,subTransactionType,fromWhere,resultType) {
  var that = this;
  //取得openid
  var openid = "";
  console.log("11");
  totalMoney = money;
  orderno = payorderno;
  wx.getStorage({
    key: 'openid',
    success: function (res) {
      console.log("22");
      console.log(res.data);
      wxOpenid = res.data;
      var url = config.getBaseUrl('Pay','CreateXcxOrder');
      var postDic = new Object();
      postDic.OrderNo = payorderno;
      postDic.UserId = app.globalData.TUserInfo.UserID;
      postDic.Subject = 'WXMini'
      postDic.Amount = money;
      postDic.TransactionType = transactionType;
      postDic.SubTransactionType = subTransactionType;

      requestDic = postDic;
      console.log("postDic:" + util.logObject(postDic));
      netutil.post(
        url,
        postDic,
        function (res) {
          console.log("gowxmini:" + util.logObject(res.data));
          if (res.data) {
            prepayid = res.data.prepayid;
            console.log(res.data);
            //获取到参数，发起支付
            that.goWXPay(res.data.Data,fromWhere,resultType);
          }
        },
        function (res) {
          console.log("gowxfail" + util.logObject(res.data));
        }
      )
    },
    fail: function (res) {
      console.log(res);
    }
  })
}

// 获取参数成功，调起微信支付
function goWXPay(payInfo,fromWhere,resultType) {
  var that = this;
  // 微信支付
  wx.requestPayment({
    timeStamp: payInfo.TimeStamp,
    nonceStr: payInfo.NonceStr,
    package: "prepay_id=" + payInfo.PrepayId,
    signType: 'MD5',
    paySign: payInfo.PaySign,
    success: function (res) {
      console.log("success");
      console.log(res);
      // if (res.data.errMsg == 'requestPayment:ok') {
      that.jumpToPaySuccessView(fromWhere,resultType);
      // }
    },
    fail: function (res) {
      console.log("fail");
      console.log(res);
      that.jumpToPayFailView(fromWhere,isfrom,resultType);
    }, complete: function (res) {
      console.log("complete");
      console.log(res);
    }
  })
}

//支付成功后跳转
function jumpToPaySuccessView(Type,resultType) {
  wx.navigateTo({
    url: '../success/success?orderN=' + orderno + '&paytype=' + Type + '&money=' + totalMoney + '&resultType=' + resultType
  })
}
//支付失败后跳转
function jumpToPayFailView(Type,comefrom,resultType) {
  if (comefrom == '0') {
    wx.navigateTo({
      url: '../failure/failure?orderN=' + orderno + '&paytype=' + Type + '&money=' + totalMoney + '&resultType=' + resultType + '&requestDic=' + JSON.stringify(requestDic)
    })
  }else {
    wx.redirectTo({
      url: '../failure/failure?orderN=' + orderno + '&paytype=' + Type + '&money=' + totalMoney + '&resultType=' + resultType + '&requestDic=' + JSON.stringify(requestDic),
    })
  }  
}

module.exports = {
  // getOrderInfoByOrderid: getOrderInfoByOrderid,
  jumpToPayFailView: jumpToPayFailView,
  jumpToPaySuccessView: jumpToPaySuccessView,
  goWXPay: goWXPay,
  getPayInfo: getPayInfo
}